/*
* 创建目录弹窗
* */
import {Modal} from 'antd';
import Draggable from 'react-draggable';
import useModel from "@/hooks/useModel.ts";
import {ReactNode, forwardRef, useImperativeHandle, MutableRefObject} from "react";

interface IProps {
    ref?: MutableRefObject<null>
    btn?: ReactNode
    content: ReactNode
    footer?: ReactNode | null
    title?: string
    close?: () => void
}

const CommonModel = forwardRef(({ btn, content, title, footer = null, close }: IProps, ref) => {
    const { open, disabled, setDisabled, bounds, draggleRef, showModal, handleOk, handleCancel, onStart } = useModel()
    useImperativeHandle(ref, () => ({
        showModal: showModal,
        handleOk: (cb?: () => void) => handleOk(cb),
        handleCancel: (cb?: () => void) => handleCancel(cb)
    }));
    return (
        <>
            { btn }
            <Modal
                title={
                    <div
                        style={{ width: '100%', cursor: 'move' }}
                        onMouseOver={() => {
                            if (disabled) {
                                setDisabled(false);
                            }
                        }}
                        onMouseOut={() => { setDisabled(true) }}
                        onFocus={() => {}}
                        onBlur={() => {}}
                    >
                        { title }
                    </div>
                }
                open={open}
                onOk={() => handleOk()}
                onCancel={close}
                footer={ footer }
                modalRender={(modal) => (
                    <Draggable
                        disabled={disabled}
                        bounds={bounds}
                        nodeRef={draggleRef}
                        onStart={(event, uiData) => onStart(event, uiData)}
                    >
                        <div ref={draggleRef}>{modal}</div>
                    </Draggable>
                )}
            >
                { content }
            </Modal>
        </>
    )
})

export default CommonModel